package com.signal;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class Tela7 extends Activity implements OnItemClickListener{
	
	private ListView mListView;
	private Button mButton;
	private EditText mEditText;
	
	private int mClickedItemIndex = -1;
	private String mClickedString = "";
	
	private String[] itens;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.layout_tela7);
		
		mListView = (ListView) findViewById(R.id.tela7_list);
		mButton = (Button) findViewById(R.id.tela7_button_continuar);
		mEditText = (EditText) findViewById(R.id.tela7_edittext_pesquisa);
		
		//alimentando lista
		itens = getResources().getStringArray(R.array.array_servicos);
		
		ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.simple_list_layout,R.id.item_simple,itens);
		
		mListView.setAdapter(arrayAdapter);
		mListView.setOnItemClickListener(this);
		
		mEditText.addTextChangedListener(new MyTextWatcher(mListView));
		//mEditText.setRawInputType(Configuration.KEYBOARD_12KEY);
		
		mButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent i = new Intent(Tela7.this,Tela8.class);
				startActivity(i);
				if(mClickedItemIndex != -1){
					SharedResources.sServicoRealizado = (String)mListView.getAdapter().getItem(mClickedItemIndex);
					System.out.println("Selecionei " + SharedResources.sServicoRealizado);
				}else{
					//TODO
					System.out.println("Alertar que nenhum item foi selecionado");
				}
			}
			
		});
		
		
		
		mListView.setOnItemClickListener(new OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView<?> adp, View arg1, int position,
					long arg3) {
				
				
				if(position != mClickedItemIndex){
					adp.getChildAt(position).setBackgroundColor(Color.GRAY);
					
					if(mClickedItemIndex < adp.getChildCount() && mClickedItemIndex != -1){
						adp.getChildAt(mClickedItemIndex).setBackgroundColor(Color.WHITE);
					}
					
					mClickedItemIndex = position;
					
				}else{
					adp.getChildAt(position).setBackgroundColor(Color.WHITE);
					mClickedItemIndex = -1;
				}
				
			}
			
		});
		
	}
	
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		
	}
	
	
	
	
	
	public class MyTextWatcher implements TextWatcher {

		ListView listView;
		//String 
		
		
		public MyTextWatcher(ListView list){
			listView = list;
		}
		
	    public void afterTextChanged(Editable editable) { 
	    	
	    }

	    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

	    }

	    public void onTextChanged(CharSequence s, int start, int before, int count) {
	    	
	    	
	    	if(!s.toString().trim().equals("")){
	    		//armazenara as linhas que possuem texto dentro do conteudo buscado
	    		ArrayList<String> listaExistentes = new ArrayList<String>();

	    		for(int x=0; x<listView.getAdapter().getCount(); x++){

	    			String line = (String)listView.getAdapter().getItem(x);

	    			//System.out.println("Line " + line + "  contains " + s.toString().toUpperCase());
	    			
	    			if(line.contains(s.toString().trim().toUpperCase())){
	    				listaExistentes.add(line);
	    			}

	    		}

	    		String[] arrayNovasLinhas = new String[listaExistentes.size()];
	    		for(int x=0; x<listaExistentes.size(); x++){
	    			arrayNovasLinhas[x] = listaExistentes.get(x);
	    		}
	    		
	    		ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(Tela7.this, R.layout.simple_list_layout,R.id.item_simple,arrayNovasLinhas);
	    		mListView.setAdapter(arrayAdapter);
	    		
	    	}else{
	    		
	    		mClickedItemIndex = -1;
	    		
	    		ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(Tela7.this, R.layout.simple_list_layout,R.id.item_simple,itens);
	    		
	    		mListView.setAdapter(arrayAdapter);
	    	}
	    	
	    }

	}

	
}
